<?php

class Zax_App_Default_File
{
	
	protected $_filename;
	protected $_path;
	protected $_actions = array();
	protected $_writing_allowed = true;
	//Méthodes de gestion du fichier
	
	/**
	 * Définit le fichier pour lequel l'App a été appelée
	 * Méthode appellée par le noyau.
	 * 
	 * @param $path
	 * @return unknown_type
	 */
	final function File_Load($path)
	{
		global $Zax;
		if (substr_count($path,'\\') !== 0) die('!\\');
		
		$path = explode('/',$path);
		$filename = array_pop($path);
		$dir = implode('/',$path);
		$dir = strtr($dir,'.','_');
		
		$this->_path = $dir.'/'.$filename;
		$this->_filename = ZAX_DATA_PATH.'/'.$dir.'/'.$filename; 
		//$this->_writing_allowed = $Zax->acl->get($path,'w');
		return true;
	}
	
	/**
	 * Retourne la metadonnée $name
	 * @param $name
	 * @return La métadonnée
	 */
	final function File_getMetadata($name)
	{
		global $Zax;
		return $Zax->meta->get($this->_path,$name);
	}
	
	/**
	 * Définit une métadonnée
	 * @param $name
	 * @param $value
	 */
	final function File_setMetadata($name,$value)
	{
		global $Zax;
		return $Zax->meta->set($this->_path,$name,$value);
	}
	
	/**
	 * Retourne le contenu du fichier
	 * @return Données
	 */
	final function File_getData()
	{
		return file_get_contents($this->_filename);
	}
	
	/**
	 * Remplace les données du fichier par $data
	 * @param $data Données à écrire
	 * @return unknown_type
	 */
	final function File_putData($data)
	{
		@mkdir(dirname($this->_filename),null,true);
		return file_put_contents($this->_filename,$data);
	}
	
	
	/*
	 * Méthodes de configuration de l'Application
	 */
	
	final function App_setAction($id,$name,$writes)
	{
		$action = array();
		$App_name = str_replace("Zax_App_",'',get_class($this));
		$file1 = ZAX_SYSTEM_PATH."/Apps/$App_name/action_$id.icon.png";
		$file2 = ZAX_SYSTEM_PATH."/Apps/Default_File/action_$id.icon.png";
		if(file_exists($file1))
			$action['icon'] = "Zax/Apps/$App_name/action_$id.icon.png";
		elseif(file_exists($file2))
			$action['icon'] = "Zax/Apps/Default_File/action_$id.icon.png";
		else
			$action['icon'] = null;
			
		$action['name'] = $name;
		$action['id'] = $id;
		$action['writes'] = $writes;
		
		$this->_actions[] = $action;
	}
	
	final function App_getActionsToolbar()
	{

		$toolbar = '<ul class="app_toolbar">';
		foreach($this->_actions as $action)
		{
			//if($Zax->acl->get($this->_path,$action['id']))
			if(1)
			{
				//style="background-image:url('.$action['icon'].')"
				$toolbar.= '<li>';
				$toolbar.= '<a  style="background-image:url('.$action['icon'].');" 
							class="button" href="?'.$this->_path.':'.$action['id'].'">';
				$toolbar.= $action['name'].'</a></li>';
			}
		}
		$toolbar.= '</ul>';
		
		return $toolbar;
	}
	
	/*
	 * Actions de bases (peuvent être surchargées)
	 */
	
	
	/**
	 * Action: print
	 * Affiche le contenu du fichier texte
	 * @return unknown_type
	 */
	public function Action_print()
	{
		
		return '<div class="plaintext">'.
		nl2br(htmlentities($this->File_getData()))
		.'</div>';
		
	}
	
	public function Action_download()
	{
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false);
		header("Content-Type: application/octet-stream");
		header("Content-Disposition: attachment; filename=\"".basename($this->_filename)."\";");
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".@filesize($this->_filename));
		readfile($this->_filename);
	}

}